package com.eyeem.recyclerviewtools.sample;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.Toast;

import com.eyeem.recyclerviewtools.adapter.WrapAdapter;
import com.eyeem.recyclerviewtools.sample.adapter.DataAdapter;
import com.eyeem.recyclerviewtools.sample.adapter.FragmentAdapter;
import com.eyeem.recyclerviewtools.sample.behavior.QuickReturnHeaderBehavior;
import com.eyeem.recyclerviewtools.sample.fragment.ViewPagerFragment;

public class ViewPagerHeaderBehaivorActivity extends BaseActivity implements QuickReturnHeaderBehavior.QuickReturnHeaderBehaviorListener {
    private SwipeRefreshLayout swipeRefreshLayout;
    private DataAdapter adapter;
    private WrapAdapter wrapAdapter;
    //private RecyclerView recycler;

    private ViewPager viewpager;
    //private View holder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager_header_behaivor);
        setTitle(null);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //添加菜单和返回按钮
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        //recycler = (RecyclerView) findViewById(recycler);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        //holder = findViewById(R.id.holderView);
        swipeRefreshLayout.setEnabled(false);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        // Adapter
        adapter = DataAdapter.generateRandom(this);
        wrapAdapter = new WrapAdapter(adapter);
        //ViewPager
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
        adapter.addFragment(ViewPagerFragment.newInstance(), ViewPagerFragment.class.getName() + 0);
        adapter.addFragment(ViewPagerFragment.newInstance(), ViewPagerFragment.class.getName() + 1);
        adapter.addFragment(ViewPagerFragment.newInstance(), ViewPagerFragment.class.getName() + 2);
        adapter.addFragment(ViewPagerFragment.newInstance(), ViewPagerFragment.class.getName() + 3);
        adapter.addFragment(ViewPagerFragment.newInstance(), ViewPagerFragment.class.getName() + 4);
        viewpager.setAdapter(adapter);
        //viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(toolbar_tab));
        Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                String msg = "";
                switch (menuItem.getItemId()) {
                    case R.id.action_settings:
                        msg += "设置";
                        break;
                }

                if (!msg.equals("")) {
                    Toast.makeText(ViewPagerHeaderBehaivorActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        };
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
        dismissRefresh(swipeRefreshLayout);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_space, menu);
        return true;
    }

    //int marginTop;

    @Override
    public void onHide(View child) {
        viewpager.animate().translationY(-child.getHeight()).setInterpolator(new AccelerateInterpolator(2)).start();
        ViewGroup.MarginLayoutParams lps = (ViewGroup.MarginLayoutParams) viewpager.getLayoutParams();
        lps.bottomMargin = -child.getHeight();
        viewpager.requestLayout();
    }

    @Override
    public void onShow(View child) {
        viewpager.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
    }
}
